<template>
  <div class="com-container" @dblclick="revertMap">
    <div ref="scatterMapRef" class="com-chart" />
  </div>
</template>
<script>
// import { getMap } from '../api/sys'
import { getProvinceMapInfo } from '../util/index'
import axios from 'axios'
import { mapState } from 'vuex'
export default {
  data() {
    return {
      scatterMap: null,
      allData: null,
      mapObj: {}
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      this.scatterMap.dispose() // 销毁当前的图表
      this.initChart() // 更换主题后重新渲染
      this.screenAdaptation() // 屏幕适配
      this.upDataChart() // 更新图表数据
    }
  },
  created() {
    this.$socket.registerCallBack('mapData', this.getData)
  },
  async mounted() {
    await this.initChart()
    this.$socket.send({
      action: 'getData',
      socketType: 'mapData',
      chartName: 'map',
      value: ''
    })
    // await this.getData()
    window.addEventListener('resize', this.screenAdaptation)
    this.screenAdaptation()
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdaptation)
    this.$socket.unRegisterCallBack('mapData')
  },
  methods: {
    async initChart() {
      this.scatterMap = this.$eCharts.init(this.$refs.scatterMapRef, this.theme)
      const res = await axios.get('http://localhost:3002/static/map/china.json')
      this.$eCharts.registerMap('china', res.data)
      const initOption = {
        title: {
          text: '▎ 商家分布',
          left: 20,
          top: 20
        },
        geo: {
          type: 'map',
          map: 'china',
          top: '5%',
          bottom: '5%',
          itemStyle: {
            areaColor: '#2E72BF',
            borderColor: '#333'
          }
        },
        legend: {
          left: '5%',
          bottom: '5%',
          orient: 'vertical'
        }
      }
      this.scatterMap.setOption(initOption)
      this.scatterMap.on('click', async arg => {
        const info = getProvinceMapInfo(arg.name)

        if (!info.key) {
          return
        }
        // 需要获取这个省份的地图矢量数据
        // 判断当前所点击的这个省份的地图矢量数据在mapData中是否存在
        if (!this.mapObj[info.key]) {
          const { data } = await axios.get('http://192.168.31.40:3002' + info.path)
          this.mapObj[info.key] = data
          this.$eCharts.registerMap(info.key, data)
        }
        const option = {
          geo: {
            map: info.key
          }
        }
        this.scatterMap.setOption(option)
      })
    },
    getData(res) {
      this.allData = res
      this.upDataChart()
    },
    upDataChart() {
      const legendArray = this.allData.map(item => {
        return item.name
      })
      // 如果想在地图中显示散点的数据, 我们需要给散点的图表增加一个配置, coordinateSystem:geo
      const seriesArray = this.allData.map(item => {
        return {
          type: 'effectScatter',
          coordinateSystem: 'geo',
          name: item.name,
          data: item.children,
          rippleEffect: {
            scale: 5,
            brushType: 'stroke'
          }
        }
      })
      const dataOption = {
        legend: {
          data: legendArray
        },
        series: seriesArray
      }
      this.scatterMap.setOption(dataOption)
    },
    screenAdaptation() {
      const titleFontSize = this.$refs.scatterMapRef.offsetWidth / 100 * 3.6
      const screenOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize / 1.2
          }
        },
        legend: {
          itemWidth: titleFontSize / 2,
          itemHeight: titleFontSize / 2,
          itemGap: titleFontSize / 2,
          textStyle: {
            fontSize: titleFontSize / 2
          }
        }
      }
      this.scatterMap.setOption(screenOption)
      this.scatterMap.resize()
    },
    // 重置回中国地图
    revertMap() {
      const option = {
        geo: {
          map: 'china'
        }
      }
      this.scatterMap.setOption(option)
    }
  }
}
</script>
